package com.teemor.tim.command.handler.c2c;

import com.alibaba.fastjson.JSON;
import com.teemor.common.bean.utils.BeanConvertor;
import com.teemor.tim.command.model.c2c.BeforeSendMsgCommand;
import com.teemor.tim.bean.result.callback.CallBackResult;
import com.teemor.tim.command.annation.CommandMapping;
import com.teemor.tim.utils.MessageUtil;
import com.teemor.tim.processor.MessageProcessor;
import com.teemor.tim.model.message.MessageWrapper;
import com.teemor.tim.model.message.StoreMessageWrapper;
import com.teemor.tim.model.message.TimMessage;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ename zhoulk
 * @cname 周李科
 * @date 2020/12/9 16:23
 */
@CommandMapping("C2C")
@Slf4j
public class C2cCommandHandler {

    @Resource
    private MessageProcessor messageProcessor;

    @CommandMapping("CallbackBeforeSendMsg")
    public CallBackResult callbackBeforeSendMsg(BeforeSendMsgCommand command) {
        log.info("Call C2C.CallbackBeforeSendMsg: {}", JSON.toJSON(command));

        //存储消息
        List<TimMessage> messageWrappers = command.getMsgBody();
        List<MessageWrapper> messages = MessageUtil.messagesFormat(messageWrappers);

        StoreMessageWrapper storeMessageWrapper = BeanConvertor.convertBean(command, StoreMessageWrapper.class);
        storeMessageWrapper.setMessageModel(messages);
        messageProcessor.storeMessage(storeMessageWrapper);

        return new CallBackResult();
    }

}
